package view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;

import listener.CutGlassPaneListener;
import model.Layer;

public class CutGlassPane extends KoalaGlassPane {
    private int m_curX, m_curY, m_leftPointX, m_leftPointY, m_decalX, m_decalY;
    private JButton m_confirmButton;
    private JButton m_cancelButton;

    public CutGlassPane() {
        super();
        m_curX = 0;
        m_curY = 0;

        m_confirmButton = new JButton(new ImageIcon(this.getClass()
                .getResource("/images/" + "valid.png")));
        m_cancelButton = new JButton(new ImageIcon(this.getClass().getResource(
                "/images/" + "cancel.png")));

        m_confirmButton.setName("confirm");
        m_cancelButton.setName("cancel");

        add(m_confirmButton);
        add(m_cancelButton);

        m_confirmButton.addActionListener(new CutGlassPaneListener());
        m_cancelButton.addActionListener(new CutGlassPaneListener());

        m_confirmButton.setVisible(true);
        m_cancelButton.setVisible(true);

        this.addMouseMotionListener(new CutGlassPaneListener());
        this.addMouseListener(new CutGlassPaneListener());
    }

    @Override
    public void paintComponent(Graphics g) {
        BufferedImage bImg = new BufferedImage(getWidth(), getHeight(),
                BufferedImage.TYPE_INT_ARGB);

        for (int x = 0; x < getWidth(); x++) {
            for (int y = 0; y < getHeight(); y++) {
                bImg.setRGB(x, y, new Color(255, 255, 255, 100).getRGB());
            }
        }
        
        g.drawImage(bImg, 0, 0, getWidth(), getHeight(), this);

        g.drawRect(m_leftPointX, m_leftPointY, m_curX
                - m_leftPointX, m_curY - m_leftPointY);

        super.paintComponent(g);
    }

    /**
     * Draw a resize rectangle from the upper left corner to the mouse position <br>
     * (if it isn't inferior than the upper left corner)
     * 
     * @param x
     * @param y
     */
    public void drawACutRect(int x, int y) {
        Layer selectedLayer = MainFrame.getInstance().getDrawingPane().getSelectedLayer();
        
        if ( x-m_decalX <= selectedLayer.getX()+selectedLayer.getWidth() ){
            m_curX = (x > m_leftPointX) ? x : m_leftPointX + 1;
        }else{
            m_curX = selectedLayer.getX()+selectedLayer.getWidth()+m_decalX;
        }
        
        if ( y-m_decalY < selectedLayer.getY()+selectedLayer.getHeight()){
            m_curY = (y > m_leftPointY) ? y : m_leftPointY + 1;
        }else{
            m_curY = selectedLayer.getY()+selectedLayer.getHeight()+m_decalY;
        }
        
        repaint();
    }
    
    public void setTopLeftCorner(int x, int y){
        Layer selectedLayer = MainFrame.getInstance().getDrawingPane().getSelectedLayer();
        
        m_decalX = MainFrame.getInstance().getDrawingPane().getX()+3;

        m_decalY =  MainFrame.getInstance().getDrawingPane().getY() 
        + MainFrame.getInstance().getMainPanel().getY()
        + MainFrame.getInstance().getScrollingPane().getY() + 3;        
        
        if ( x-m_decalX >= selectedLayer.getX()){
            m_leftPointX = x;
        }else{
            m_leftPointX = selectedLayer.getX() + m_decalX;
        }
        
        if (y-m_decalY >= selectedLayer.getY()){
            m_leftPointY = y;
        }else{
            m_leftPointY = selectedLayer.getY() + m_decalY;
        }
    }

    /**
     * send the coord at the top left corner
     * 
     * @return
     */
    public Point getTopLeftPoint() {
        Layer layerSelect = MainFrame.getInstance().getDrawingPane().getSelectedLayer();
        return new Point(m_leftPointX - m_decalX - layerSelect.getX(), m_leftPointY - m_decalY - layerSelect.getY());
    }
    
    /**
     * send the coord at the bottom right corner
     * 
     * @return
     */
    public Point getBottomRightPoint() {
        Layer layerSelect = MainFrame.getInstance().getDrawingPane().getSelectedLayer();
        return new Point(m_curX - m_decalX - layerSelect.getX(), m_curY - m_decalY - layerSelect.getY());
    }
    
    
}
